// src/router/index.js

import Vue from 'vue'
//1. 引入路由包
import Router from "vue-router"

// import Home from '../views/Home.vue'
//路由懒加载（不可以命名）===>最常用
const Home = () => import(/* webpackChunkName:'Home'*/ '@/views/Home')
// import DetailVue from '../views/DetailVue.vue'
import Register from '../views/Register.vue'
import GoodsCart from '../views/GoodsCart.vue'
import Mine from '../views/Mine.vue'
// import Sort from '../views/Sort.vue'
const Sort = () => import(/*webpackChunkName:'Sort'*/ '@/views/Sort')
// import SearchList from '../views/SearchList.vue'
import Login from '../views/Login.vue'
import Address from '../views/Address.vue'
import Collect from '../views/Collect.vue'
import PhoneRegister from '../views/PhoneRegister.vue'
import SearchIndex from '../views/SearchIndex.vue'
import PhoneTest from '../views/PhoneTest.vue'
import Setting from '../views/Setting.vue'
import PhoneRegisterPsd from '../views/PhoneRegisterPsd.vue'
import NotFound from '../views/NotFound.vue'
import SeeMenu from '../views/SeeMenu.vue'
// import Submit from '../views/Submit.vue'
const Submit = () => import(/*webpackChunkName:'Submit'*/'../views/Submit.vue')

const AboutUs = () => import(/*webpackChunkName:'AboutUs'*/ '../views/AboutUs.vue')
// import AboutUs from '../views/AboutUs.vue'
const AddressAdd = () => import(/*webpackChunkName:'AddressAdd'*/'../views/AddressAdd.vue')
//2. 安装插件包到Vue上, 
Vue.use(Router)

//3. 路由配置


//4.路由实例
//5.导出路由实例，让它去控制vue根
let router = new Router({ //插件路由对象
    // routes:routes
    // mode:'hash'//哈希模式   location.href
    // mode:'history',//历史记录   history.pushState
    routes: [
        {
            path: '/',
            redirect: '/Home'
        },
        {
            path: '/Home',
            component: Home
        },
        {
            path: '/DetailVue/:id',
            // component: DetailVue,

            //路由懒加载（可以命名）
            component: r => require.ensure([], () => r(require('@/views/DetailVue')), 'DetailVue'),
            name: 'DetailVue',
        },

        {
            path: '/GoodsCart',
            component: GoodsCart,
            /*   meta: {
                  requiresAuth: true
              }, */
            name: 'GoodsCart',
              beforeEnter: (to, from, next) => {
                 if (!localStorage.getItem("token")) {
                     next("/Login");
                 }
                 next()
             }
        },
        {
            path: '/Register',
            component: Register
        },
        {
            path: '/Mine',
            component: Mine,
            //路由独享守卫
            /*  beforeEnter: (to, from, next) => {
                 if (!localStorage.getItem("token")) {
                     next("/Login");
                 }
                 next()
             } */
        },
        {
            path: '/Sort',
            component: Sort
        },
        {
            path: '/SearchList/:goodsSearch',
            //路由懒加载（不可以命名）
            component: resolve => require(['@/views/SearchList'], resolve),
        },
        {
            path: '/Login',
            component: Login,
        },
        {
            path: '/Address',
            component: Address,
        },
        {
            path: '/Collect',
            component: Collect
        },
        {
            path: '/PhoneRegister',
            component: PhoneRegister
        },
        {
            path: '/SearchIndex',
            component: SearchIndex
        },
        {
            path: '/PhoneTest',
            component: PhoneTest
        },
        {
            path: '/Setting',
            component: Setting
        },
        {
            path: '/PhoneRegisterPsd',
            component: PhoneRegisterPsd
        },
        {
            path: '/Submit/:time',
            component: Submit
        },
        {
            path: '/AboutUs',
            component: AboutUs
        },
        {
            path: '/SeeMenu',
            component: SeeMenu
        },
        {
            path: '/AddressAdd',
            component: AddressAdd
        },
        {
            path: '*',
            component: NotFound
        },
    ]
});

//路由全局守卫
/* router.beforeEach((to, from, next) => {
    if (to.meta) {
        if (!localStorage.getItem("token")) {
            next("/Login?path=" + to.path)
        }
    }
    next();
}) */
export default router;